# Copyright 2018- The Pixie Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0

load("@rules_cc//cc:defs.bzl", "cc_binary")
load("//bazel:pl_build_system.bzl", "pl_cc_library", "pl_cc_test")

package(default_visibility = ["//src:__subpackages__"])

pl_cc_library(
    name = "cc_library",
    srcs = glob(
        ["*.cc"],
        exclude = [
            "**/*_test.cc",
            "**/*_tool.cc",
        ],
    ),
    hdrs = glob(["*.h"]),
    linkopts = ["-lz"],
    deps = [
        "//third_party:libarchive",
    ],
)

pl_cc_test(
    name = "minitar_test",
    srcs = ["minitar_test.cc"],
    data = [
        "testdata/a.tar.gz",
        "testdata/ref/bar",
        "testdata/ref/foo",
    ],
    deps = [
        ":cc_library",
        "//src/common/exec:cc_library",
    ],
)

# This is minitar tool is a stand-alone tar extractor.
# Current uses are for manual testing and as back plan to extracting linux headers.
# TODO(oazizi): Can probably kill this soon.
# Make sure not to use pl_cc_binary, because the binary becomes 21 MB instead of 460KB.
cc_binary(
    name = "minitar",
    srcs = ["minitar_tool.c"],
    defines = [
        "NO_BZIP2",
        "NO_COMPRESS",
        "NO_CREATE",
        "NO_CPIO_EXTRACT",
        "NO_LOOKUP",
    ],
    linkopts = ["-lz"],
    deps = ["//third_party:libarchive"],
)
